package com.arnold.www.volatiles;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName volatileTest
 * @Description: volatile简单展示线程可见性
 * @Author Arnold
 * @Date 2020/4/12
 * @Version V2.0
 **/
public class VolatileTest {

    private volatile boolean result = false;


    public static void main(String[] args) {
        final VolatileTest volatileTest = new VolatileTest();

        ExecutorService executorService = Executors.newFixedThreadPool(2);

        executorService.submit(new Runnable() {
            public void run() {
                for (int i=0;i<10;i++) {
                    System.out.println(Thread.currentThread().getName() + "第 " + i + "次执行");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                volatileTest.result=true;
            }
        });

        executorService.submit(new Runnable() {
            public void run() {
               while (!volatileTest.result){
               }
                System.out.println("第二个线程执行结束！！！");
            }
        });
        executorService.shutdown();
    }
}